package cn.getech.data.development.controller;

import cn.getech.data.development.constant.LevelTypeEnum;
import cn.getech.data.development.constant.PermissTypeEnum;
import cn.getech.data.development.entity.AllPermissionConfig;
import cn.getech.data.development.entity.DataPermissionParam;
import cn.getech.data.development.service.IAllPermissionConfigService;
import cn.getech.data.development.service.IDataPermissionParamService;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.service.SysUserRoleService;
import cn.getech.system.center.utils.ShiroUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 安全全局配置 前端控制器
 * </p>
 *
 * @author nxy
 * @since 2019-12-18
 */
@RestController
@RequestMapping("/bdp/allPermissionConfig")
@Api("安全级别全局设置")
public class AllPermissionConfigController {
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private IAllPermissionConfigService allPermissionConfigService;
    @Autowired
    private IDataPermissionParamService dataPermissionParamService;

    @ApiOperation("查询")
    @GetMapping("/select")
    @SysLog(value = "安全级别全局设置查询数据", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE)
    public R getLevelInfo(){
        Long uid = ShiroUtils.getUserId();
        if (sysUserRoleService.superAdmin(uid)){
            AllPermissionConfig tconfig = allPermissionConfigService.getConfigByPermissType(PermissTypeEnum.TABLE_TYPE.getPermissType());
            String table = LevelTypeEnum.getLevelById(tconfig.getDataPermissionId());
            AllPermissionConfig fconfig = allPermissionConfigService.getConfigByPermissType(PermissTypeEnum.FIELD_TYPE.getPermissType());
            String feilds = LevelTypeEnum.getLevelById(fconfig.getDataPermissionId());
            List<DataPermissionParam> levels = dataPermissionParamService.list(new QueryWrapper<DataPermissionParam>().eq("permiss_type",PermissTypeEnum.FIELD_TYPE.getPermissType()).or().eq("permiss_type",PermissTypeEnum.TABLE_TYPE.getPermissType()));

            Map<String, String> tableMap = new HashMap<>();
            Map<String, String> feildsMap = new HashMap<>();
            for (DataPermissionParam level : levels){
                if (level.getPermissType() == PermissTypeEnum.TABLE_TYPE.getPermissType()){
                    tableMap.put(LevelTypeEnum.getLevelById(level.getId()),level.getPermissName());
                }else {
                    feildsMap.put(LevelTypeEnum.getLevelById(level.getId()),level.getPermissName());
                }
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("table",table);
            jsonObject.put("feild",feilds);
            jsonObject.put("tableLevels",tableMap);
            jsonObject.put("feildLevels",feildsMap);
            return R.okWithData(jsonObject);
        }
        return R.ok("没有访问该模块权限！");
    }


    @ApiOperation("修改")
    @PostMapping("/update")
    @SysLog(value = "安全级别全局设置修改设置", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    @Transactional(rollbackFor = Exception.class)
    public R updateLevelInfo(@RequestBody Map<String, Object> map){
        Long uid = ShiroUtils.getUserId();
        if (sysUserRoleService.superAdmin(uid)){
            String table = (String) map.get("table");
            String feild = (String) map.get("feild");
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(map.get("levels")));

            List<AllPermissionConfig> allPermissionConfigs = new LinkedList<>();
            AllPermissionConfig tconfig = allPermissionConfigService.getConfigByPermissType(PermissTypeEnum.TABLE_TYPE.getPermissType());
            AllPermissionConfig fconfig = allPermissionConfigService.getConfigByPermissType(PermissTypeEnum.FIELD_TYPE.getPermissType());
            List<DataPermissionParam> levels = dataPermissionParamService.list(new QueryWrapper<DataPermissionParam>().eq("permiss_type",PermissTypeEnum.FIELD_TYPE.getPermissType()).or().eq("permiss_type",PermissTypeEnum.TABLE_TYPE.getPermissType()));

            allPermissionConfigs.add(tconfig.setDataPermissionId(LevelTypeEnum.getIdByLevel(table)));
            allPermissionConfigs.add(fconfig.setDataPermissionId(LevelTypeEnum.getIdByLevel(feild)));
            for (DataPermissionParam level : levels){
                String name = (String) jsonObject.get(LevelTypeEnum.getLevelById(level.getId()));
                level.setPermissName(name);
            }

            allPermissionConfigService.updateBatchById(allPermissionConfigs);
            dataPermissionParamService.updateBatchById(levels);
        }
        return R.ok();
    }

}

